package io;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

// Tools to read an write files as single strings:
public class TextFile extends ArrayList {

	public static String read(String fileName) throws IOException {
		StringBuffer sb = new StringBuffer();
		BufferedReader in = new BufferedReader(new FileReader(fileName));
		String s;
		while (null != (s = in.readLine())) {
			sb.append(s).append('\n');
		}
		in.close();
		return sb.toString();
	}

	private List<String> fileLineList;

	public TextFile(String fileName) throws IOException {
		fileLineList = new ArrayList<String>(Arrays.asList(read(fileName)
				.split("\n")));
	}
	
	public void write(String fileName) throws IOException {
		PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(fileName)));
		for(int i=0; i<fileLineList.size(); i++) {
			out.println(fileLineList.get(i));
		}
		out.close();
	}
	
	public static void main(String[] args) throws Exception{
		System.out.println(read("src/io/TextFile.java"));
		
		System.out.println(System.getProperty("user.dir"));
	}
}
